//
//  PostAttributedString.m
//  XPhoto
//
//  Created by sentinel on 2020/3/20.
//  Copyright © 2020 baboy. All rights reserved.
//

#import "CommonAttributedString.h"
#import <BKKit/Theme.h>

@implementation CommonAttributedString
+ (NSMutableAttributedString *) contentAttributedStringWithText:(NSString *)text{
    NSMutableAttributedString *contentAttrStr = [BKAttributedString attrString:text
                                                            font:THEME_TEXT_COMMENT_FONT
                                                           color:THEME_TEXT_COMMENT_COLOR];
    //匹配@的用户
    [BKAttributedString setAttributedString:contentAttrStr withRegexp:@"@([^\\s@#]+)" withLinkType:APP_LINK_TYPE_USER];
    //匹配##之间的话题，或者只有前缀#也可以
    [BKAttributedString setAttributedString:contentAttrStr withRegexp:@"#([^\\s#@]+)#?" withLinkType:APP_LINK_TYPE_TOPIC];
    [BKAttributedString setAttributedString:contentAttrStr withRegexp:@"(https?://[\\w\\d\\.\\-]+\\.[\\w]{2,4}(?:\\d+)?(?:/[\\w\\d\\.\\-~!@#$%^&*+?:_/=<>]*)?)" withLinkType:APP_LINK_TYPE_HTTP];
    return contentAttrStr;
}

+ (NSMutableAttributedString *) contentAttributedStringWithPost:(Post *)post{
    NSMutableAttributedString *authorAttrStr = [BKAttributedString attrString:[NSString stringWithFormat:@"%@ ", post.user.nickname]
                                                           font:[UIFont boldSystemFontOfSize:THEME_TEXT_COMMENT_FONT.pointSize]
                                                          color:THEME_TEXT_COLOR];
    NSMutableAttributedString *contentAttrStr = [BKAttributedString attrString:post.content
                                                            font:THEME_TEXT_COMMENT_FONT
                                                           color:THEME_TEXT_COMMENT_COLOR];
    //匹配@的用户
    [BKAttributedString setAttributedString:contentAttrStr withRegexp:@"@([^\\s@#]+)" withLinkType:APP_LINK_TYPE_USER];
    //匹配##之间的话题，或者只有前缀#也可以
    [BKAttributedString setAttributedString:contentAttrStr withRegexp:@"#([^\\s#@]+)#?" withLinkType:APP_LINK_TYPE_TOPIC];
    
    [authorAttrStr appendAttributedString:contentAttrStr];
    return authorAttrStr;
}

/**
 评论数字符串处理
 */
+ (NSMutableAttributedString *) commentCountAttributedStringWithPost:(Post *)post{
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] init];
    if (post.likeCount > 0) {
        NSMutableAttributedString *likeCountAttrStr = [BKAttributedString attrString:[NSString stringWithFormat:@"%ld ", post.likeCount]
                                                                  font:THEME_TEXT_COMMENT_FONT
                                                                 color:THEME_TEXT_COLOR];
        NSMutableAttributedString *likeDescAttrStr = [BKAttributedString attrString:NSLocalizedString(@"like_count_suffix", @"帖子")
                                                                 font:THEME_TEXT_COMMENT_FONT
                                                                color:THEME_TEXT_COMMENT_COLOR];
        [likeCountAttrStr appendAttributedString:likeDescAttrStr];
        NSString *param = [NSString stringWithFormat:@"%ld", post.id];
        NSString *link = APP_LINK(APP_LINK_TYPE_LIKE, param);
        [likeCountAttrStr addAttribute:NSLinkAttributeName value:link range:NSMakeRange(0, likeDescAttrStr.length)];
        [attrStr appendAttributedString:likeCountAttrStr];
        
    }
    /**
    设置评论数文字属性
    */
    if (post.commentCount>0) {
        if( post.likeCount > 0){
            [attrStr appendAttributedString:[BKAttributedString attrString:@", " font:THEME_TEXT_COMMENT_FONT color:THEME_TEXT_COMMENT_COLOR]];
        }
        NSMutableAttributedString *commentCountAttrStr = [BKAttributedString attrString:[NSString stringWithFormat:@"%ld ", post.commentCount]
                                                                  font:THEME_TEXT_COMMENT_FONT
                                                                 color:THEME_TEXT_COLOR];
        NSMutableAttributedString *commentDescAttrStr = [BKAttributedString attrString:NSLocalizedString(@"comment_count_suffix", @"帖子")
                                                                 font:THEME_TEXT_COMMENT_FONT
                                                                color:THEME_TEXT_COMMENT_COLOR];
        
        
        [commentCountAttrStr appendAttributedString:commentDescAttrStr];
        NSString *param = [NSString stringWithFormat:@"%ld", post.id];
        NSString *link = APP_LINK(APP_LINK_TYPE_COMMENT, param);
        [commentCountAttrStr addAttribute:NSLinkAttributeName value:link range:NSMakeRange(0, commentCountAttrStr.length)];
        [attrStr appendAttributedString:commentCountAttrStr];
    }
    return attrStr.length > 0 ? attrStr : nil;
}


+ (NSMutableAttributedString *) commentAttributedStringWithComment:(Comment *)comment{
    NSMutableAttributedString *nickNameAttrStr = [BKAttributedString attrString: [NSString stringWithFormat:@"%@ ", comment.user.nickname]
                                                                           font: [UIFont boldSystemFontOfSize:THEME_TEXT_COMMENT_FONT.pointSize]
                                                                          color: THEME_TEXT_COLOR];
    
    NSString *path = [NSString stringWithFormat:@"/?uid=%ld",comment.uid];
    [nickNameAttrStr addAttribute:NSLinkAttributeName value:APP_LINK(APP_LINK_TYPE_USER, path) range:NSMakeRange(0, nickNameAttrStr.length)];
    
    NSMutableAttributedString *contentAttrStr = [BKAttributedString attrString: comment.content
                                                                          font: THEME_TEXT_COMMENT_FONT
                                                                         color: THEME_TEXT_COMMENT_COLOR];
    
//    path = [NSString stringWithFormat:@"/?id=%ld",comment.id];
//    [contentAttrStr addAttribute:NSLinkAttributeName value:APP_LINK(APP_LINK_TYPE_COMMENT, path) range:NSMakeRange(0, contentAttrStr.length)];
    //匹配@的用户
    [BKAttributedString setAttributedString:contentAttrStr withRegexp:@"@([^\\s@#]+)" withLinkType:APP_LINK_TYPE_USER];
    //匹配##之间的话题，或者只有前缀#也可以
    [BKAttributedString setAttributedString:contentAttrStr withRegexp:@"#([^\\s#@]+)#?" withLinkType:APP_LINK_TYPE_TOPIC];
    
    [nickNameAttrStr appendAttributedString:contentAttrStr];
    return nickNameAttrStr;
}
@end
